package com.sahyadri.web.bean;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * BaseUser object represents a user of the system. Implements the Spring
 * {@link UserDetails} interface so that in can be used in the security context. <br />
 * Application specific User classes should extend this class to add application
 * specific properties so that all user attributes will be available in the
 * {@link SecurityContext}
 * <p>
 * Because this class is used within the security framework and will most likely
 * be cached so it is implemented as an immutable object.
 * 
 */

@MappedSuperclass
public class BaseUser implements UserDetails, Serializable {
	private static final long serialVersionUID = -345743479832530576L;
	
	
	private String username;
	
	@Transient
	private Collection<GrantedAuthority> authorities;

	/**
	 * Default constructor
	 */
	public BaseUser() {
	}

	public BaseUser(String username) {
		this.username = username;
	}

	public BaseUser(String username, Collection<GrantedAuthority> authorities) {
		this.username = username;
		this.authorities = authorities;
	}

	@Override
	public Collection<GrantedAuthority> getAuthorities() {
		return this.authorities;
	}

	@Override
	public String getUsername() {
		return this.username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @param authorities
	 *            the authorities to set
	 */
	public void setAuthorities(Collection<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	
	// **************************************************************************
	// Methods below are simply to satisfy the Spring UserDetails contract.
	// **************************************************************************
	@Override
	public String getPassword() {
		return "N/A";
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("BaseUser [authorities=");
		builder.append(authorities);
		builder.append(", username=");
		builder.append(username);
		builder.append("]");
		return builder.toString();
	}

}
